<template>
  <div class="add">
    <el-card>
      <div>
        <el-icon><Tickets /></el-icon>数据列表
      </div>
      <el-button type="primary" @click="showDialog = true">添加</el-button>
    </el-card>
  </div>
  <el-table
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
    :data="list"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="类型名称" prop="name" />
    <el-table-column label="属性数量" prop="attributeCount" />
    <el-table-column label="参数数量" prop="paramCount" />
    <el-table-column label="设置">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'allocRole'"
          @click="attributeList(row.id, row.name, 0)"

          >属性列表</el-button
        >
        <el-button text type="primary" v-permission="'allocRole'"
          >参数列表</el-button
        >
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'allocRole'"
          @click="editUser(row.id,row.name)"
          >编辑</el-button
        >
        <el-button
          text
          type="primary"
          v-permission="'allocRole'"
          @click="open(row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
      />
    </div>
  </ElConfigProvider>
  <ProduteditEdit :show="showDialog" @chean="closeDialog" :id="editId" :name="editName" />
</template>
<script setup lang="ts">
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { Tickets } from "@element-plus/icons-vue";
import { productAttributeApi, productAttrDeleteApi } from "./api/api";
import { useRoute } from "vue-router";
import useTable from "@/hooks/useTable";
import { reactive, ref } from "vue";
import ProduteditEdit from "./components/productedit.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const {
  list,
  total,
  pageNum,
  pageSize,
  pageChangeHandle,
  pageSizeChangeHandle,
  searchParamsFn,
} = useTable(productAttributeApi);
// 添加按钮
const showDialog = ref(false);
const closeDialog = () => {
  showDialog.value = false;
  searchParamsFn()
};
// 编辑按钮
const editId = ref(0);
const editName = ref<string>()
const editUser = (id: number,name: string,) => {
  showDialog.value = true;
  editName.value = name;
  editId.value = id;
  searchParamsFn()
};
// 删除
const openId = ref(0);
const open = (id: number) => {
  ElMessageBox.confirm("是否删除改品牌", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      productAttrDeleteApi(id).then((res) => {
        // 删除数据更新
        if (res.code === 0) {
          ElMessage.success("删除成功");
          searchParamsFn();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消成功",
      });
    });
};
// 属性列表
const attributeList = (id: number, name: string, type: number) => [
  router.push(`/pms/productAttrList?cid=${id}&name=${name}&type=${type}`),
];
</script>
<style lang="less" scoped>
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
}
:deep(.cell) {
  text-align: center;
}
.page {
  display: flex;
  justify-content: flex-end;
  padding-top: 20px;
}
</style>
